package bingo.common.utils;

import java.io.IOException;
import java.io.InputStream;

import net.bingosoft.common.util.FileHelper;

/**
 * Jul 26, 2008 8:37:24 AM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:调用windows 可执行文件,批处理文件的工具类
 * ChangeLog:
 */
public class CmdExecutor {
	
	private CmdExecutor() {}
	/**
	 * 执行可执行命令
	 * @param cmd
	 * @return
	 * @throws IOException
	 */
	public static String execute(String cmd) throws IOException{
		//执行命令 
		Process process = Runtime.getRuntime().exec(cmd); 
        //取得命令结果的输出流 
        InputStream fis = process.getInputStream(); 
        String output = FileHelper.readStreamContent(fis,System.getProperty("file.encoding"));
        return output;
	}
	/**
	 * 用系统默认的编辑器打开指定的文件
	 * @param filePath
	 * @throws IOException
	 */
	public static void openFile(String filePath) throws IOException{
		execute("cmd /c start " + filePath);
	}
	/**
	 * 创建目录
	 * @param dirPath
	 * @throws IOException
	 */
	public static void makeDir(String dirPath) throws IOException{
		execute("cmd /c md " + dirPath);
	}
	/**
	 * 文件重命名
	 * @param srcName
	 * @param destName
	 * @throws IOException
	 */
	public static void renameFile(String srcName,String destName) throws IOException{
		execute("cmd /c rename " + srcName + " " + destName);
	}
	/**
	 * 移动文件
	 * @param srcName
	 * @param destName
	 * @throws IOException
	 */
	public static void moveFile(String srcName,String destName) throws IOException{
		execute("cmd /c move " + srcName + " " + destName);
	}
	/**
	 * 删除文件
	 * @param filePath
	 * @throws IOException
	 */
	public static void delFile(String filePath) throws IOException{
		execute("cmd /c del " + filePath);
	}
	/**
	 * 展示目录下面的内容
	 * @param dir
	 * @return
	 * @throws IOException
	 */
	public static String dir(String dir) throws IOException{
		return execute("cmd /c dir " + dir);
	}
	
	/**
	 * 强制删除指定目录下面的所有文件和目录
	 * @param dir
	 * @throws IOException
	 */
	public static void delDir(String dir) throws IOException{
		execute("cmd /c rd /s /q " + dir);
	}
	
}
